package com.orifine.orm.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.Assert;

import com.orifine.orm.SqlScriptFactory;
import com.orifine.utils.ScannerUtils;

public class TableScannerConfigurer implements InitializingBean, BeanFactoryPostProcessor {
	private String basePackage;

	private SqlScriptFactory sqlScriptFactory;

	public String getBasePackage() {
		return basePackage;
	}

	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	public SqlScriptFactory getSqlScriptFactory() {
		return sqlScriptFactory;
	}

	public void setSqlScriptFactory(SqlScriptFactory sqlScriptFactory) {
		this.sqlScriptFactory = sqlScriptFactory;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		Assert.notNull(basePackage, "basePackage is Null");
		Assert.notNull(sqlScriptFactory, "sqlScriptFactory is Null");
		
		sqlScriptFactory.setTableClasses(ScannerUtils.getClasses(basePackage));
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0) throws BeansException {
		sqlScriptFactory.execute();
	}
}
